package com.avo.ramcache.persist;

import com.avo.ramcache.config.IEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

public class Element {

    private static final Logger LOGGER = LoggerFactory.getLogger(Element.class);
    private EventType type;
    private final Serializable id;
    private IEntity<?> entity;
    private final Class<? extends IEntity<?>> entityClass;

    public static Element saveOf(IEntity<?> entity) {
        return new Element(EventType.SAVE, entity.getId(), entity, (Class<? extends IEntity<?>>) entity.getClass());
    }

    public static Element removeOf(Serializable id, Class<? extends IEntity<?>> entityClass) {
        return new Element(EventType.REMOVE, id, null, entityClass);
    }

    public static Element updateOf(IEntity<?> entity) {
        return new Element(EventType.UPDATE, entity.getId(), entity, (Class<? extends IEntity<?>>) entity.getClass());
    }

    public Element(EventType type, Serializable id, IEntity<?> entity, Class<? extends IEntity<?>> entityClass) {
        this.type = type;
        this.id = id;
        this.entity = entity;
        this.entityClass = entityClass;
    }

    public String getIdentity() {
        return this.entityClass.getName() + ":" + this.id;
    }

    public boolean update(Element element) {
        this.entity = element.getEntity();
        switch (this.type) {
            case SAVE:
                switch (element.type) {
                    case SAVE:
                        LOGGER.error("更新元素异常，实体[{}]原状态[{}]当前状态[{}]不进行修正", new Object[]{this.getIdentity(), this.type, element.getType()});
                        return true;
                    case UPDATE:
                        if (LOGGER.isDebugEnabled()) {
                            LOGGER.debug("实体[{}]原状态[{}]当前状态[{}]修正后状态[{}]是否保留队列元素[{}]", new Object[]{this.getIdentity(), EventType.SAVE, element.getType(), this.type, true});
                        }
                        return true;
                    case REMOVE:
                        if (LOGGER.isDebugEnabled()) {
                            LOGGER.debug("实体[{}]原状态[{}]当前状态[{}]修正后状态[{}]是否保留队列元素[{}]", new Object[]{this.getIdentity(), EventType.SAVE, element.getType(), this.type, false});
                        }
                        return false;
                    default:
                        return true;
                }
            case UPDATE:
                switch (element.type) {
                    case SAVE:
                        LOGGER.error("更新元素异常，实体[{}]原状态[{}]当前状态[{}]不进行修正", new Object[]{this.getIdentity(), this.type, element.getType()});
                        return true;
                    case UPDATE:
                        if (LOGGER.isDebugEnabled()) {
                            LOGGER.debug("实体[{}]原状态[{}]当前状态[{}]修正后状态[{}]是否保留队列元素[{}]", new Object[]{this.getIdentity(), EventType.SAVE, element.getType(), this.type, true});
                        }
                        return true;
                    case REMOVE:
                        if (LOGGER.isDebugEnabled()) {
                            LOGGER.debug("实体[{}]原状态[{}]当前状态[{}]修正后状态[{}]是否保留队列元素[{}]", new Object[]{this.getIdentity(), EventType.SAVE, element.getType(), this.type, true});
                        }
                        return true;
                    default:
                        return true;
                }
            case REMOVE:
                switch (element.type) {
                    case SAVE:
                        this.type = EventType.UPDATE;
                        if (LOGGER.isDebugEnabled()) {
                            LOGGER.debug("实体[{}]原状态[{}]当前状态[{}]修正后状态[{}]是否保留队列元素[{}]", this.getIdentity(), EventType.SAVE, element.getType(), this.type, true);
                        }
                        break;
                    case UPDATE:
                        LOGGER.error("更新元素异常，实体[{}]原状态[{}]当前状态[{}]不进行修正", this.getIdentity(), this.type, element.getType());
                        break;
                    case REMOVE:
                        LOGGER.error("更新元素异常，实体[{}]原状态[{}]当前状态[{}]不进行修正", this.getIdentity(), this.type, element.getType());
                        break;
                }
        }
        return true;
    }


    public EventType getType() {
        return this.type;
    }

    public Class<? extends IEntity<?>> getEntityClass() {
        return this.entityClass;
    }

    public Serializable getId() {
        return id;
    }

    public IEntity<?> getEntity() {
        return this.entity;
    }

    public String toString() {
        return type + " ID:" + id;
    }
}
